//
//  StockInfoModelViewController.m
//  HeXun
//
//  Created by 8003 on 15/11/24.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "StockInfoModelViewController.h"
#import "StockInfoViewModel.h"
#import "StockModel.h"
#import "GetXMLObject.h"
#import "FundViewModel.h"
@interface StockInfoModelViewController ()
{
    NSArray* _stockInfoArr;
    NSArray* _points;
    StockModel* _IDModel;
    NSInteger _isEqual;
    NSString* _stockFilePath;
}
@end

@implementation StockInfoModelViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
       self.view.backgroundColor = [UIColor whiteColor];
    
    StockModel* stockModel = self.pointArr;
    if (self.segmentIndex != 4 && self.segmentIndex != 3 && self.segmentIndex != 5 && self.segmentIndex != 6)
    {
        GetXMLObject* getXMLObject = [GetXMLObject new];
        StockInfoViewModel* stockInfoViewModel = [[StockInfoViewModel alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, self.view.frame.size.height-20)];
        [self.view addSubview:stockInfoViewModel];
        NSArray* arr = [getXMLObject getStockInfoArrayFromStockID:stockModel.stockID];
        NSMutableArray* tempArr = [NSMutableArray new];
        _IDModel = arr[0];
        for (int i = 1 ; i < arr.count ; i++)
        {
            StockModel* stock = arr[i];

            NSLog(@"%@",[NSString stringWithFormat:@"%ld",stock.tradingPriceSum.integerValue]);

            NSInteger a = pow(10, [NSString stringWithFormat:@"%ld",stock.tradingPriceSum.integerValue].length-2);
            CGFloat tradingPrice = stock.tradingPriceSum.integerValue / a;
            NSLog(@"%lf",tradingPrice);
            CGPoint point = CGPointMake(i + 50, tradingPrice);
            NSValue* pointValue = [NSValue valueWithCGPoint:point];
            [tempArr addObject:pointValue];
        }
        _points = [NSArray arrayWithArray:tempArr];
        NSLog(@"%@",_points);
        [stockInfoViewModel drawLine:_points];
        UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(50,280, 300, 2)];
        label.backgroundColor = [UIColor blackColor];
        [self.view addSubview:label];
        UILabel* label2 = [[UILabel alloc]initWithFrame:CGRectMake(50,130, 2, 150)];
        label2.backgroundColor = [UIColor blackColor];
        [self.view addSubview:label2];
        
        UILabel* time = [[UILabel alloc]initWithFrame:CGRectMake(290, 300, 50, 20)];
        time.text = @"time";
        [self.view addSubview:time];
        UILabel* priceSum = [[UILabel alloc]initWithFrame:CGRectMake(10 , 130, 40, 20)];
        priceSum.text = @"pri";
        [self.view addSubview:priceSum];
        
        UILabel* name = [[UILabel alloc]initWithFrame:CGRectMake(50 , 310, 100, 50)];
        name.text = stockModel.stockName;
        [self.view addSubview:name];
        
    }
    else
    {
        FundViewModel* fundViewModel = [[FundViewModel alloc]initWithFrame:CGRectMake(10, 150, self.view.frame.size.width - 10, self.view.frame.size.height - 150)];
        _IDModel = stockModel;
        fundViewModel.textName.text = stockModel.stockName;
        fundViewModel.textNewst.text = stockModel.newest;
        fundViewModel.textTime.text = stockModel.fld_time;
        [self.view addSubview:fundViewModel];
    }
    
    
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(80, 70, 100, 50)];
    button.tag = 1;
    [button setTitle:@"关注" forState:UIControlStateNormal];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor blackColor];
    [button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton* button2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 70, 100, 50)];
    button2.tag = 2;
    [button2 setTitle:@"取消关注" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    button2.backgroundColor = [UIColor blackColor];
    [button2 addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];



}

- (void)buttonPress:(UIButton* )button
{
    //将json保存到沙盒
    NSString *documentsPath1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSLog(@"%ld",self.segmentIndex);
    if (self.segmentIndex == 3 || self.segmentIndex == 4 ||self.segmentIndex == 5 || self.segmentIndex == 6)
    {
        _stockFilePath = [documentsPath1 stringByAppendingPathComponent:@"fund.plist"];
        NSLog(@"fund.plist: %@", _stockFilePath);
    }
    else
    {
        _stockFilePath = [documentsPath stringByAppendingPathComponent:@"stock.plist"];
        NSLog(@"stockFilePath: %@", _stockFilePath);
    }
    
    if (button.tag == 1)
    {
        if (_IDModel.stockID == nil)
        {
            NSArray* array = [NSArray arrayWithContentsOfFile:_stockFilePath];
            if (array == nil)
            {
                array = [NSArray new];
                NSDictionary* dic = @{@"name":_IDModel.stockName,@"time":_IDModel.fld_time,@"newest":_IDModel.newest,@"upAndDown":_IDModel.upsAndDowns,@"amountrise":_IDModel.amountofIncrease};
                NSArray* array1 = [array arrayByAddingObject:dic];
                [array1 writeToFile:_stockFilePath atomically:YES];
            }
            else
            {
                for (int i = 0; i < array.count; i++)
                {
                    if ([_IDModel.stockName isEqualToString:array[i][@"name"]])
                    {
                        _isEqual = 1;
                        break;
                    }
                    else
                    {
                        _isEqual = 0;
                    }
                }
                
                if (_isEqual == 0)
                {
                    NSDictionary* dic = @{@"name":_IDModel.stockName,@"time":_IDModel.fld_time,@"newest":_IDModel.newest,@"upAndDown":_IDModel.upsAndDowns,@"amountrise":_IDModel.amountofIncrease};
                    NSArray* array1 = [array arrayByAddingObject:dic];
                    [array1 writeToFile:_stockFilePath atomically:YES];
                }
            }
        }
        else
        {
            NSArray* array = [NSArray arrayWithContentsOfFile:_stockFilePath];
            if (array == nil)
            {
                array = [NSArray new];
                NSArray* array1 = [array arrayByAddingObject:_IDModel.stockID];
                
                [array1 writeToFile:_stockFilePath atomically:YES];
            }
            else
            {
                for (int i = 0; i < array.count; i++)
                {
                    if ([_IDModel.stockID isEqualToString:array[i]])
                    {
                        _isEqual = 1;
                        break;
                    }
                    else
                    {
                        _isEqual = 0;
                    }
                }
                
                if (_isEqual == 0)
                {
                    NSArray* array1 = [array arrayByAddingObject:_IDModel.stockID];
                    
                    [array1 writeToFile:_stockFilePath atomically:YES];
                }
            }
        }
    }
    if (button.tag == 2)
    {
        if (_IDModel.stockID != nil)
        {
            NSMutableArray* array = [NSMutableArray arrayWithContentsOfFile:_stockFilePath];
            if (array == nil)
            {
                
            }
            
            for (int i = 0; i < array.count; i++)
            {
                if ([_IDModel.stockID isEqualToString:array[i]])
                {
                    _isEqual = 1;
                    
                    [array removeObjectAtIndex:i];
                    NSArray* array1 = [NSArray arrayWithArray:array];
                    [array1 writeToFile:_stockFilePath atomically:YES];
                    break;
                }
                else
                {
                    _isEqual = 0;
                }
            }
        }
        else
        {
             NSMutableArray* array = [NSMutableArray arrayWithContentsOfFile:_stockFilePath];
            if (array == nil)
            {
                
            }
            for (int i = 0; i < array.count; i++)
            {
                if ([_IDModel.stockName isEqualToString:array[i][@"name"]])
                {
                    _isEqual = 1;
                    
                    [array removeObjectAtIndex:i];
                    NSArray* array1 = [NSArray arrayWithArray:array];
                    [array1 writeToFile:_stockFilePath atomically:YES];
                    break;
                }
                else
                {
                    _isEqual = 0;
                }
            }
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
